package com.hz.filemanager.ui.file.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.hz.filemanager.R;
import com.hz.filemanager.expendable_recyclerview.BaseExpandableRecyclerViewAdapter;
import com.hz.filemanager.model.file.ImageBean;
import com.hz.filemanager.model.file.ImgFolderBean;

import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends BaseExpandableRecyclerViewAdapter<ImgFolderBean, ImageBean, ImageAdapter.GroupVH, ImageAdapter.ChildVH>{

    private Context context;
    private List<ImgFolderBean> datas = new ArrayList<>();
    private OnLongClickListener longClickListener;
    private OnClickListener onClickListener;
    private boolean isSelectModel = false;
    private List<ImageBean> selectImages = new ArrayList<>();

    public List<ImgFolderBean> getDatas() {
        return datas;
    }

    public List<ImageBean> getSelectImages() {
        return selectImages;
    }

    public void setDatas(List<ImgFolderBean> datas) {
        if(datas == null || datas.isEmpty()){
            this.datas.clear();
        }else{
            this.datas.clear();
            this.datas.addAll(datas);
        }
        
        notifyDataSetChanged();
    }

    public ImageAdapter(Context context){
        this.context = context;
    }

    public void setLongClickListener(OnLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setSelectModel(boolean selectModel) {
        isSelectModel = selectModel;
    }

    public void checkAll(boolean isCheck){
        selectImages.clear();
        if (isCheck){
            for (int i = 0; i < datas.size(); i++) {
                selectImages.addAll(new ArrayList<>(datas.get(i).getImageBeans()));
            }
        }else {

        }
        notifyDataSetChanged();
    }

    public boolean isSelectModel() {
        return isSelectModel;
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public ImgFolderBean getGroupItem(int groupIndex) {
        return datas.get(groupIndex);
    }

    @Override
    public GroupVH onCreateGroupViewHolder(ViewGroup parent, int groupViewType) {
        return new GroupVH(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listitem_group, parent, false));
    }

    @Override
    public void onBindGroupViewHolder(GroupVH holder, ImgFolderBean groupBean, boolean isExpand,int position) {
        holder.nameTv.setText(groupBean.getName());
        holder.groupSizeTv.setText("("+groupBean.getChildCount()+")");
        if (groupBean.isExpandable()) {
            holder.foldIv.setVisibility(View.VISIBLE);
            holder.foldIv.setImageResource(isExpand ? R.mipmap.icon_expand : R.mipmap.icon_no_expand);
        } else {
            holder.foldIv.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public ChildVH onCreateChildViewHolder(ViewGroup parent, int childViewType) {
        return new ChildVH(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_child, parent, false));
    }

    @Override
    public void onBindChildViewHolder(ChildVH holder, ImgFolderBean groupBean, ImageBean imageBean,int position) {
        Glide.with(context)
                .load(imageBean.getPath())
                .transform(new CenterCrop(),new RoundedCorners(25))
                .placeholder(R.color.cbcbcb)
                .error(R.color.cbcbcb)
                .into(holder.iv);

        if (isSelectModel){
            holder.cb.setVisibility(View.VISIBLE);
            holder.cb.setChecked(selectImages.contains(imageBean));
        }else {
            holder.cb.setVisibility(View.GONE);
        }

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (longClickListener != null && !isSelectModel){
                    longClickListener.onLongClick(groupBean,imageBean);
                }
                return true;
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSelectModel){
                    if(selectImages.contains(imageBean)){
                        selectImages.remove(imageBean);
                        holder.cb.setChecked(false);
                    }else{
                        selectImages.add(imageBean);
                        holder.cb.setChecked(true);
                    }
                }

                if(onClickListener != null){
                    onClickListener.onClick(v, groupBean, imageBean);
                }
            }
        });
    }

    public void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }

    static class GroupVH extends BaseExpandableRecyclerViewAdapter.BaseGroupViewHolder {
        TextView groupSizeTv;
        ImageView foldIv;
        TextView nameTv;

        GroupVH(View itemView) {
            super(itemView);
            foldIv = (ImageView) itemView.findViewById(R.id.group_item_indicator);
            nameTv = (TextView) itemView.findViewById(R.id.group_item_name);
            groupSizeTv = itemView.findViewById(R.id.group_size);
        }

        @Override
        protected void onExpandStatusChanged(RecyclerView.Adapter relatedAdapter, boolean isExpanding) {
            foldIv.setImageResource(isExpanding ? R.mipmap.icon_expand : R.mipmap.icon_no_expand);
        }
    }

    static class ChildVH extends RecyclerView.ViewHolder {
        ImageView iv;
        CheckBox cb;

        ChildVH(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.child_iv);
            cb = (CheckBox) itemView.findViewById(R.id.cb);
        }
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if(layoutManager instanceof GridLayoutManager){
            final GridLayoutManager gridManager = ((GridLayoutManager) layoutManager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (getItemViewType(position) == TYPE_GROUP) {
                        return 3; //返回3展示一行
                    } else {
                        return 1;//返回1展示3列
                    }

                }
            });
        }
    }

    public interface OnLongClickListener{
        void onLongClick(ImgFolderBean groupBean, ImageBean imageBean);
    }

    public interface OnClickListener{
        void onClick(View v, ImgFolderBean groupBean, ImageBean imageBean);
    }
}
